package br.com.solrtools.solrquerydao;

/**
 *
 * @author eleazer
 */

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

/*import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;*/

import br.com.ideais.metasolr.dao.AbstractSolrDao;
import br.com.ideais.metasolr.template.SolrTemplate;
import br.com.solrtools.bean.SolrQueryBean;

public class IndexerAndSearcher extends AbstractSolrDao<SolrQueryBean> {

	//private SolrServer solrServer;
    private SolrTemplate solrTemplate;
	
    public IndexerAndSearcher() {}
	
    /*public IndexerAndSearcher() {
		try {
			//this.solrServer = new CommonsHttpSolrServer("http://localhost:8983/solr");
                        this.solrServer = new CommonsHttpSolrServer("http://localhost:7001/solr/idxItem/admin/");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}*/
         
	
	/**
	 * 
	 * @param field
	 * @param text
	 *//*
	public void search(String field, String text) {
	    // http://localhost:8983/solr/select/q=*:*&sort=id+asc&version=2.2
	    ModifiableSolrParams params = new ModifiableSolrParams();
	    params.set("q", field + ":" + text);

		try {
			QueryResponse queryResponse = solrServer.query(params);
			
			SolrDocumentList documents = queryResponse.getResults();
			System.out.println("\n--\n");
			for(SolrDocument document : documents) {
				System.out.println("Document[" + document.getFieldValue("id") + "]");
				System.out.println("\tDocument Fields[" + document.getFieldNames() + "]");
				System.out.println("\tNome: " + document.getFieldValue("nome"));
				System.out.println("\tDescrição: " + document.getFieldValue("descricao"));
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	    
	}
	
	*//**
	 * 
	 * @param id
	 * @param nome
	 * @param descricao
	 *//*
	public void addDocument(String id, String nome, String descricao) {
		SolrInputDocument document = new SolrInputDocument();
	    document.addField("id", id);
	    document.addField("nome", nome);
	    document.addField("descricao", descricao);
	    
	    try {
			solrServer.add(document);
			solrServer.commit();
			System.out.println("[Document] Evento cadastrado: " + document.toString());
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	*//**
	 * 
	 *//*
	public void findAllDocuments() {
		SolrQuery query = new SolrQuery();
	    query.setQuery("*:*");
	    //query.addSortField("id", SolrQuery.ORDER.asc);
	    
		try {
            List<SolrQueryBean> teste = super.query(query);
			QueryResponse queryResponse = solrServer.query(query);
			
			// Recupera todos os documentos
			SolrDocumentList documents = queryResponse.getResults();
			for(SolrDocument document : documents) {
				System.out.println("Document[" + document.getFieldValue("id") + "]");
				System.out.println("\tDocument Fields[" + document.getFieldNames() + "]");
				System.out.println("\tNome: " + document.getFieldValue("nome"));
				System.out.println("\tDescrição: " + document.getFieldValue("descricao"));
			}
			
			System.out.println("\n--\n");
			
			
			
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		IndexerAndSearcher indexerAndSearcher = new IndexerAndSearcher();
		
		//indexerAndSearcher.addDocument("1", "Café com Tapioca", "Evento de Java do CEJUG em Fortaleza");
		//indexerAndSearcher.addBean("2", "EJES", "Encontro de Java do Espírito Santo");
		
		indexerAndSearcher.findAllDocuments();
		
		//indexerAndSearcher.search("id", "[2 TO 5]");
	}

    *//**
     * @return the solrTemplate
     *//*
    public SolrTemplate getSolrTemplate() {
        return solrTemplate;
    }

    *//**
     * @param solrTemplate the solrTemplate to set
     *//*
    public void setSolrTemplate(SolrTemplate solrTemplate) {
        this.solrTemplate = solrTemplate;
    }*/
	
}